// Copyright (C) 2022 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
	"errors"
	"reflect"
	"time"
)


func TsToTime(ts int64) time.Time {
	return time.UnixMilli(ts)
}

func MergeArray(dest []interface{}, src []interface{}) (result []interface{}) {
	result = make([]interface{}, len(dest)+len(src))
	copy(result, dest)
	copy(result[len(dest):], src)
	return
}

func CheckParameters(dataStruct interface{}) error {
	valueOf := reflect.ValueOf(dataStruct)
	typeOf := reflect.TypeOf(dataStruct)
	var startTime int64 = 1
	var endTime int64 = 1
	var interval int64 = 1
	var visitId int64 = 1
	for index := 0; index < typeOf.Elem().NumField(); index++ {
		field := typeOf.Elem().Field(index)
		value := valueOf.Elem().FieldByName(field.Name)
		if field.Name == "StartTime" {
			startTime = value.Int()
		} else if field.Name == "EndTime" {
			endTime = value.Int()
		} else if field.Name == "Interval" {
			interval = value.Int()
		} else if field.Name == "VisitId" {
			visitId = value.Int()
		}
	}
	if visitId < 0 {
		return errors.New("visitId is illegal")
	}
	if endTime-startTime < 0 || endTime <= 0 || startTime <= 0 {
		return errors.New("the start time must be less than the end time or time needs to be a positive number")
	}
	if interval > 6 || interval < 1 {
		return errors.New("the value of interval is out of range")
	}
	return nil
}
